package com.wsx.sky.sentinel.fisrt;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.sentinel.fisrt.UserService
 * @Description: TODO
 * @date 2019/03/06
 */
public class UserService {

    public static final String USER_RS = "userResource";
    public static final int USER_CT = 20;


    /**
     * 正常的业务请求操纵
     * @param uid
     * @return
     */
    public User getUser(long uid) {
        Entry entry = null;
        try {
            entry = SphU.entry(USER_RS);
            return new User("user_" + uid, uid);
        } catch (BlockException e) {
            System.out.println("被限流了");
        } finally {
            if (null != entry) {
                entry.exit();
            }
        }
        return null;
    }


    @NoArgsConstructor
    public static class User {
        @Setter @Getter private String name;
        @Setter @Getter private long uid = System.currentTimeMillis();;

        public User(String name, long uid) {
            this.name = name;
            this.uid = uid;
        }
    }
}
